
package com.android.generator.messageid;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import com.android.generator.whatid.CalendarManager;
import com.android.replace.TextUtils;

//TODO Same method name
public class GenerateWhat {

    private static final String HANDLER_NAME = "mCalendarHandler";

    private static final boolean HANDLE_MESSAGE_HEAD = true;

    public static void main(String[] args) {
        Field[] declaredFields = XsiMsg.class.getDeclaredFields();
        int max = 0;
        if (HANDLE_MESSAGE_HEAD) {
            System.out.println("        int what = msg.what;");
            System.out.println("        int arg1 = msg.arg1 ;");
            System.out.println("        int arg2 = msg.arg2 ;");
            System.out.println("        switch (what){");
        }
        for (Field field : declaredFields) {
            if (((field.getModifiers() & Modifier.FINAL) == Modifier.FINAL)
                    && ((field.getModifiers() & Modifier.STATIC) == Modifier.STATIC)
                    && ((field.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC)) {
                field.setAccessible(true);
                String fieldName = field.getName();
                try {
                    int object = (int) field.get(CalendarManager.class);
                    Arg annotation = field.getAnnotation(Arg.class);
                    System.out.println("            case XsiMsg." + fieldName + ":");

                    if (annotation != null) {
                        String[] obj = annotation.obj();
                        String[] objType = annotation.objType();
                        String objectName = annotation.objectName();
                        if (!(TextUtils.isEmpty(objectName)) && obj.length != objType.length && obj.length != 0) {
                            throw new IllegalArgumentException(fieldName + " obj is not right");
                        } else if (!TextUtils.isEmpty(objectName)) {
                            if (obj != null) {
                                System.out.println("                Object[] " + objectName + " = (Object[]) msg.obj;");
                                for (int i = 0; i < obj.length; i++) {
                                    System.out.println("                " + objType[i] + " " + obj[i] + " = "
                                            + objectName + "[" + i + "];");
                                }
                            }
                        }
                        if (!TextUtils.isEmpty(annotation.arg1())) {
                            System.out.println("                int " + annotation.arg1() + " = arg1 ;");
                        }
                        if (!TextUtils.isEmpty(annotation.arg2())) {
                            System.out.println("                int " + annotation.arg2() + " = arg2 ;");
                        }

                    }
                    System.out.println("                //TODO");
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        if (HANDLE_MESSAGE_HEAD) {
            System.out.println("            default:");
            System.out.println("                break;");
            System.out.println("        }");
        }
    }
}
